package com.kjtpay.design_pattern.memento;

import android.util.Log;

import java.util.Random;

/**
 * 游戏角色；就是Originator类
 */
class GameRole {
    private int vitality;//生命力
    private int attack;//攻击力
    private int defense;// 防御力
    final private int limit = 30; // 判断标准
    private boolean flag;

    /**
     * 状态显示
     */
    public String display() {
        String state = "角色当前状态：" + "\n"
                + "体力:"+ vitality + "\n"
                + "攻击力:"+ attack + "\n"
                + "防御力:"+ defense + "\n";
        if (flag == true) {
            state += "状态良好，可以继续战斗";
        } else {
            state += "状态糟糕，请注意";
        }
        Log.v("GameRole", state);
        return state;
    }

    /**
     * 获得初始状态
     */
    public void initState() {
        vitality = 100;
        attack = 100;
        defense = 100;
        flag = true;
    }

    /**
     * 战斗，与大boss决战，损耗; 这里简单表示为[0 ~ 100)之间的随机数
     */
    public void fight() {
        vitality = new Random().nextInt(100);
        attack = new Random().nextInt(100);
        defense = new Random().nextInt(100);
        if ((vitality > limit) && (attack > limit) && (defense > limit)) {
            flag = true;
        } else {
            flag = false;
        }
    }

    /**
     * 保存角色状态，将三个值通过实例化：角色状态存储箱返回
     */
    public RoleStateMemento saveState() {
        return new RoleStateMemento(vitality, attack, defense);
    }

    /**
     * 恢复角色状态
     */
    public void recoveryState(RoleStateMemento memento) {
        if (memento != null) {
            this.vitality = memento.getVitality();
            this.attack = memento.getAttack();
            this.defense = memento.getDefense();
            if ((vitality > limit) && (attack > limit) && (defense > limit)) {
                flag = true;
            } else {
                flag = false;
            }
        }
    }
}
